package mate.dart.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import mate.dart.AbstractGame;
import mate.dart.ThrowEvent;
import mate.dart.utils.Utils;

public class GameX01ScoreBoard extends AbstractGame {

	private static final long serialVersionUID = 1L;
	
	int game = 301;
	private int players = 2;
	int round = 1;
	int currentPlayer = 0;
	int throwsNumber = 0;

	String beforeRound = "";
	
	JTextField[] playersCounters = null;
	JLabel[] playersThrows = null;
	JTextField roundField = new JTextField("1");
	JLabel leftDarts = new JLabel("Left Darts: 3");
	JTextArea info = new JTextArea(" ");

	public GameX01ScoreBoard(int game) {
		this.game = game;
		initComponents();
	}
	
	public GameX01ScoreBoard() {
		this(301);
	}
	
	public String getName() {
		return game + "";
	}

	private void initComponents() {

		playersCounters = new JTextField[players];
		playersThrows = new JLabel[players];
		for (int i = 0; i < players; i++) {
			playersCounters[i] = new JTextField(this.game + "");		
			playersCounters[i].setFont(Utils.getBigFont());
			playersCounters[i].addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					info.setText(getInfoString());
				}
			});
			playersThrows[i] = new JLabel(""); 
			playersThrows[i].setFont(Utils.getSmallFont());
		}
		
		GridLayout layout = new GridLayout(4,1);
		setLayout(layout);
		
		JLabel label;
		label = new JLabel("You are playing the " + game);
		label.setFont(Utils.getSmallFont());
		add(label);
		
		Box box = Box.createHorizontalBox();
		label = new JLabel("Round: ");
		label.setFont(Utils.getSmallFont());
		box.add(label);
		box.add(Box.createHorizontalStrut(10));
		roundField.setMaximumSize(new Dimension(50, 50));
		roundField.setFont(Utils.getSmallFont());
		roundField.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				round = Integer.parseInt(roundField.getText());
			}
		});
		box.add(roundField);
		add(box);
		
		box = Box.createHorizontalBox();
		for (int i = 0; i < players; i++) {
			Box vBox = Box.createVerticalBox();
			vBox.add(playersCounters[i]);
			vBox.add(playersThrows[i]);
			box.add(vBox);
		}
		add(box);
		
		box = Box.createVerticalBox();
		leftDarts.setFont(Utils.getSmallFont());
		info.setBackground(new JPanel().getBackground());
		info.setFont(Utils.getSmallFont());
		info.setEditable(false);
		JPanel p = new JPanel();
		p.add(leftDarts);
		box.add(p);
		box.add(info);
		add(box);
		leftDarts.setText("Left Darts: 3");
		colorCounters();
	}
	
	public void newThrowEvent(ThrowEvent event) {
		throwsNumber++;
		leftDarts.setText("Left Darts: " + (3 - throwsNumber));
		if (throwsNumber == 1) {
			beforeRound = playersCounters[currentPlayer].getText();
		}
		if (throwsNumber == 1) {
			playersThrows[currentPlayer].setText(event.getPointsCount() + "");
		} else {
			playersThrows[currentPlayer].setText(
					playersThrows[currentPlayer].getText() + "  " + 
					event.getPointsCount());
		}
		if (event.isAllDarts()) throwsNumber = 3;
		
		int diff = Integer.parseInt(playersCounters[currentPlayer].getText())
				- event.getPointsCount(); 

		if (diff  > 1) {
			playersCounters[currentPlayer].setText(diff + "");
			info.setText(getInfoString());
		} else if (diff == 0) {
			if (event.isDouble()) {
				JOptionPane.showMessageDialog(getRootPane(), "Winner");
				clear();
				return;
			} else {
				playersCounters[currentPlayer].setText(beforeRound);
				throwsNumber = 3;
			}
		} else if (diff <= 1) {
			playersCounters[currentPlayer].setText(beforeRound);
			throwsNumber = 3;
		}

		if (throwsNumber == 3) {
			if (currentPlayer == players - 1) incRound();
			changePlayer();
		}
	}
	
	public void clear() {
		throwsNumber = 0;
		currentPlayer = 0;
		round = 0;
		incRound();
		for (JTextField f : playersCounters) {
			f.setText(game + "");
		}
		for (JLabel l : playersThrows) {
			l.setText(" ");
		}
		leftDarts.setText("Left Darts: 3");
		info.setText("");
		colorCounters();
	}
	
	private void changePlayer() {
		currentPlayer = (currentPlayer + 1 ) % players;
		throwsNumber = 0;
		leftDarts.setText("Left Darts: 3");
		info.setText(getInfoString());
		colorCounters();
	}
	
	private String getInfoString() {
		String[] points = Utils.getCheckout(playersCounters[currentPlayer].getText());
		if (points == null) {
			return "";
		}
		StringBuilder builder = new StringBuilder();
		for (String s : points) {
			builder.append("\n").append(s);
		}
		return builder.toString();
	}
	
	public void setPlayers(int players) {
		removeAll();
		this.players = players;
		initComponents();
		leftDarts.setText(""); //to force repainting
		clear();
	}
	
	private void incRound() {
		round++;
		roundField.setText(round + "");
	}
	
	private void colorCounters() {
		for (JTextField f : playersCounters) {
			if (playersCounters[currentPlayer] == f) {
				f.setBackground(Color.GREEN);
			} else {
				f.setBackground(Color.WHITE);
			}
		}
	}
}
